package com.iwhalecloud.uncc.dao.impl;

import com.iwhalecloud.uncc.dao.ArchiverDao;
import com.iwhalecloud.uncc.db.DatasourceContextHolder;
import com.iwhalecloud.uncc.model.InsDataLog;
import com.iwhalecloud.uncc.utils.BatchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author zq
 * @date 2021/10/24 11:26
 * @description
 */
@Repository("insDataLogArchiverDao")
public class InsDataLogArchiverDaoImpl implements ArchiverDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Object query(Object o) {
        return null;
    }

    @Override
    public List queryList(Object o) {
        StringBuilder sql = new StringBuilder("SELECT * FROM INS_DATA_LOG WHERE TASK_ID = ?");
        List<InsDataLog> list = jdbcTemplate.query(sql.toString(), new Object[]{o} , new BeanPropertyRowMapper<>(InsDataLog.class));
        if (!list.isEmpty()) {
            return list;
        }
        return null;
    }

    @Override
    public List batchQuery(List list) {
        StringBuilder sql = new StringBuilder("SELECT");
        sql.append(" `ID`, `TASK_ID`, `CMPT_ID`, `DATA_TIME`, `ACTION_TYPE`, `REDIS_KEY`, `CONTENT`, `PARTITION_CODE`");
        sql.append(" FROM INS_DATA_LOG WHERE TASK_ID in (:i)");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        List<InsDataLog> res = BatchUtil.batchQueryBeans(sql,list,new RowMapper() {
            @Override
            public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                InsDataLog insDataLog = new InsDataLog();
                insDataLog.setId(resultSet.getLong("ID"));
                insDataLog.setTaskId(resultSet.getString("TASK_ID"));
                insDataLog.setCmptId(resultSet.getString("CMPT_ID"));
                insDataLog.setDateTime(resultSet.getString("DATE_TIME"));
                insDataLog.setActionType(resultSet.getInt("ACTION_TYPE"));
                insDataLog.setRedisKey(resultSet.getString("REDIS_KEY"));
                insDataLog.setContent(resultSet.getString("CONTENT"));
                insDataLog.setPartitionCode(resultSet.getString("PARTITION_CODE"));
                return insDataLog;
            }
        });
        return res;
    }

    @Override
    public void insert(Object o) {

    }

    @Override
    public void batchInsert(List list) {
        StringBuilder sql = new StringBuilder("INSERT INTO `ins_data_log`");
        sql.append(" (`ID`, `TASK_ID`, `CMPT_ID`, `DATA_TIME`, `ACTION_TYPE`, `REDIS_KEY`, `CONTENT`, `PARTITION_CODE`, `ARCHIVER_TIME`)");
        sql.append(" VALUES (:id, :taskId, :cmptId, :dateTime, :actionType, :redisKey, :content, :partitionCode, :archiverTime)");
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void update(int flag, Object o) {

    }

    @Override
    public void batchUpdate(int flag, List list) {
        StringBuilder sql = new StringBuilder("UPDATE INS_COLLECT_TASK SET ARCHIVER_FLAG = ");
        sql.append(flag);
        sql.append(" WHERE ID = :id");
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void delete(Object o) {

    }

    @Override
    public void batchDelete(List list) {
        StringBuilder sql = new StringBuilder("DELETE FROM INS_DATA_LOG WHERE ID = :id");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }
}
